package com.centaline.androidsalesblog.base;

import android.content.Context;
import android.support.v7.widget.AppCompatTextView;
import android.view.LayoutInflater;
import android.view.View;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.model.LatLngBounds;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.iml.BaiduMarkerClick;

import java.util.ArrayList;
import java.util.List;

/**
 * 租售\新房\小区Overlay
 * Created by guilin on 16/2/17.
 */
public abstract class BaseEstOverlay<T> implements BaiduMap.OnMarkerClickListener {

    protected final BaiduMap baiduMap;
    protected final BaiduMarkerClick baiduMarkerClick;
    protected View estView;

    protected AppCompatTextView atv_mark_title;
    protected ArrayList<T> data;

    protected List<Overlay> overlayList;

    public BaseEstOverlay(Context context, BaiduMap baiduMap, BaiduMarkerClick baiduMarkerClick) {
        this.baiduMap = baiduMap;
        this.baiduMarkerClick = baiduMarkerClick;
        if (overlayList == null)
            overlayList = new ArrayList<>();
        estView = LayoutInflater.from(context).inflate(R.layout.layout_est_marker, null);
        atv_mark_title = (AppCompatTextView) estView.findViewById(R.id.atv_mark_title);
    }

    /**
     * 设置数据
     */
    public void setData(ArrayList<T> data) {
        this.data = data;
    }

    /**
     * 将所有Overlay 添加到地图上
     */
    public final void addToMap() {
        if (checkBaiduMap())
            return;
        if (data == null)
            return;
        removeFromMap();
        for (T t : data) {
            if (checkInBound(t)) {
                overlayList.add(baiduMap.addOverlay(getMarkerOptions(t)));
            }
        }
        zoomToSpan();
    }

    /**
     * 检查是否在最大范围内
     */
    protected abstract boolean checkInBound(T t);

    /**
     * 设置MarkerOptions
     *
     * @param t T
     * @return MarkerOptions
     */
    public abstract MarkerOptions getMarkerOptions(T t);

    /**
     * 移除所有Overlay
     */
    public void removeFromMap() {
        for (Overlay overlay : overlayList) {
            overlay.remove();
        }
        overlayList.clear();
    }

    /**
     * 缩放地图，使所有Overlay都在合适的视野内
     */
    private void zoomToSpan() {
        if (checkBaiduMap()) {
            return;
        }
        if (overlayList.size() > 0) {
            LatLngBounds.Builder builder = new LatLngBounds.Builder();
            for (Overlay overlay : overlayList) {
                // polyline 中的点可能太多，只按marker 缩放
                if (overlay instanceof Marker) {
                    builder.include(((Marker) overlay).getPosition());
                }
            }
            baiduMap.animateMapStatus(MapStatusUpdateFactory
                    .newLatLngBounds(builder.build()));
        }
    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        return overlayList != null &&
                overlayList.contains(marker) &&
                baiduMarkerClick.onMarkerClick(marker);
    }

    /**
     * 检查BaiduMap
     */
    protected boolean checkBaiduMap() {
        return baiduMap == null;
    }
}
